package com.gidgabs.utils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.gidgabs.common.exception.RRException;
import com.gidgabs.common.utils.DateUtils;
import com.gidgabs.config.GidgabsContants;
import org.apache.commons.lang.StringUtils;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by tby on 2018/7/13.
 */
public class YunXinUtil {

    //云信注册url
    public static final String REGIST_URL="https://api.netease.im/nimserver/user/create.action";
    public static final String UPDATE_URL="https://api.netease.im/nimserver/user/updateUinfo.action";
    /**单聊查询*/
    public static final String QUERY_MSG_URL="https://api.netease.im/nimserver/history/querySessionMsg.action";

    /**群聊查询*/
    public static final String QUERY_TEAM_MSG="https://api.netease.im/nimserver/history/queryTeamMsg.action";
    public static final Map<String,String> headers=new HashMap<>();
    private static final ObjectMapper objectMapper=new ObjectMapper();
    static {
        headers.put("AppKey", GidgabsContants.YUN_XIN_APP_KEY);
        headers.put("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
    }

    private static void updateNonce(){
        String nonce= String.valueOf(Math.random());
        String curTime = String.valueOf((new Date()).getTime() / 1000L);
        String checkSum = CheckSumBuilder.getCheckSum(GidgabsContants.YUN_XIN_APP_SECRET, nonce ,curTime);//参考 计算CheckSum的java代码
        headers.put("Nonce", nonce);
        headers.put("CurTime", curTime);
        headers.put("CheckSum", checkSum);
    }

    public static String registYunxin(String accid,String name){
        Map<String,String> params=new HashMap<>();
        params.put("accid",accid);
        params.put("name",name);
        updateNonce();
        String result = HttpsUtil.post(REGIST_URL, params, headers);
        Map<String,Object> resultMap=converStringToMap(result);
        if (resultMap.get("code").equals(200)){
            return String.valueOf(((Map)resultMap.get("info")).get("token"));
        }else {
            //注册运行账户失败
            throw new RRException(result);
        }
    }

    public static void updateUserInfo(String accid,String name,String icon,String sign,String email,String birth,String mobile,Integer gender){
        Map<String,String> params=new HashMap<>();
        if (StringUtils.isNotBlank(accid)){
            params.put("accid",accid);
        }
        if (StringUtils.isNotBlank(name)){
            params.put("name",name);
        }
        if (StringUtils.isNotBlank(icon)){
            params.put("icon",icon);
        }
        if (StringUtils.isNotBlank(sign)){
            params.put("sign",sign);
        }
        if (StringUtils.isNotBlank(email)){
            params.put("email",email);
        }
        if (StringUtils.isNotBlank(birth)){
            params.put("birth",birth);
        }
        if (StringUtils.isNotBlank(mobile)){
            params.put("mobile",mobile);
        }
        if (gender!=null){
            params.put("gender",String.valueOf(gender));
        }
        updateNonce();
        HttpsUtil.post(UPDATE_URL, params, headers);

    }

    /**
     * 查询单聊历史消息记录
     * @return
     */
    public static Map<String,Object> querySessionMsg(String from,String to,Date startTime,Date endTime,Integer limit,Integer reverse,String type){
        Map<String,String> params=new HashMap<>();
        params.put("begintime",String.valueOf(startTime.getTime()));
        params.put("endtime",String.valueOf(endTime.getTime()));
        params.put("from",String.valueOf(from));
        params.put("to",String.valueOf(to));
        params.put("reverse",String.valueOf(reverse));
        params.put("limit",String.valueOf(limit));
        if (StringUtils.isNotBlank(type))
            params.put("type",String.valueOf(type));
        updateNonce();
        String result = HttpsUtil.post(QUERY_MSG_URL, params, headers);
        return converStringToMap(result);
    }

    /**
     * 查询群聊消息
     * @param tid
     * @param accid
     * @param startTime
     * @param endTime
     * @param limit
     * @param reverse
     * @param type
     * @return
     */
    public static Map<String,Object>  queryTeamMsg(String tid,String accid,Date startTime,Date endTime,Integer limit,Integer reverse,String type){

        Map<String,String> params=new HashMap<>();
        params.put("begintime",String.valueOf(startTime.getTime()));
        params.put("endtime",String.valueOf(endTime.getTime()));
        params.put("tid",String.valueOf(tid));
        params.put("accid",accid);
        params.put("reverse",String.valueOf(reverse));
        params.put("limit",String.valueOf(limit));
        if (StringUtils.isNotBlank(type))
            params.put("type",String.valueOf(type));
        updateNonce();
        String result = HttpsUtil.post(QUERY_TEAM_MSG, params, headers);
        return converStringToMap(result);
    }


    private static Map<String,Object> converStringToMap(String result) {
        try {
            return objectMapper.readValue(result,Map.class);
        } catch (IOException e) {
            throw new RRException("转化map失败"+result);
        }
    }

    public static void main(String[] args) throws JsonProcessingException {
        Date preDay = DateUtils.preDay(30);

        Map<String, Object> stringObjectMap = queryTeamMsg("607132422", "391", preDay, new Date(), 100, 2, null);
        System.out.println(objectMapper.writeValueAsString(stringObjectMap));
    }

}
